package com.iotechn.unimall.data.dto.excel;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;

import java.io.Serializable;
import java.text.DecimalFormat;

/**
 * sku总价
 *
 * @author kyms
 * @date 2020-08-05 15:38
 */
@ExcelTarget("skuTotalPriceDTO")
@Data
public class SkuTotalPriceDTO implements Serializable {

    private String barCode;
    private Long skuId;
    @Excel(name = "数量", isStatistics = true)
    private Float num = 0f;
    @Excel(name = "单价", isStatistics = true)
    private Float price = 0f;
    @Excel(name = "金额", isStatistics = true)
    private Float totalPrice = 0f;

    public Float getPrice() {
        if (this.price == null) {
            return 0F;
        }
        DecimalFormat decimalFormat = new DecimalFormat(".000");
        String total = decimalFormat.format(this.price);
        int index = firstIndexOf(total, ".");
        String substring = total.substring(0, index + 3);
        return Float.valueOf(substring);
    }

    public Float getNum() {
        if (this.num == null) {
            return 0F;
        }
        DecimalFormat decimalFormat = new DecimalFormat(".000");
        String total = decimalFormat.format(this.num);
        return Float.valueOf(total);
    }

//    public Float getTotalPrice() {
//        if (this.num == null || this.price == null||this.totalPrice==null) {
//            return 0F;
//        }
//        DecimalFormat decimalFormat = new DecimalFormat(".00");
//        String total = decimalFormat.format(this.num * this.price);
//        return Float.valueOf(total);
//    }


    public SkuTotalPriceDTO(Float price) {

        this.price = price;
    }

    public SkuTotalPriceDTO() {
    }

    public SkuTotalPriceDTO(Float num, Float price, Float totalPrice) {
        this.num = num;
        this.price = price;
        this.totalPrice = totalPrice;
    }


    public static int firstIndexOf(String str, String pattern) {
        for (int i = 0; i < (str.length() - pattern.length()); i++) {
            int j = 0;
            while (j < pattern.length()) {
                if (str.charAt(i + j) != pattern.charAt(j))
                    break;
                j++;
            }
            if (j == pattern.length())
                return i;
        }
        return -1;
    }
}
